package com.cyandpos.pad.cateringformat.opentable;

import android.util.Log;

import com.cyandpos.pad.common.commondata.BaseResponse;
import com.cyandpos.pad.common.commondata.ErpCommonData;
import com.cyandpos.pad.common.netmanager.PosHttpManager;
import com.cyandpos.pad.http.OkHttpUtils;

import com.cyandpos.pad.posdata.DinnerTable;
import com.cyandpos.pad.posdata.TableData;
import com.cyandpos.pad.posdata.TableReserve;
import com.cyandpos.pad.posdata.TableReserveData;
import com.cyandpos.pad.utils.CommonUtils;
import com.cyandpos.pad.utils.ToastUtils;
import com.google.gson.JsonObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xin on 2021/9/7
 * desc:
 */
public class ModelFragmentOpenTable implements ImplModelFragmentOpenTable {

    /**
     * 查询餐桌状态
     * @param tableState
     * @param onQueryTableStatusListener
     */
    @Override
    public void queryTableStatusInfo(int tableState,final onQueryTableStatusListener onQueryTableStatusListener) {
        PosHttpManager.querytablesHttpEexcute(tableState,new OkHttpUtils.ResultCallback() {
            @Override
            public void onSuccess(JsonObject response) {
                BaseResponse<TableData> baseResponse = BaseResponse.fromJson(response.toString(), TableData.class);

                if(baseResponse.getCode().equals("0")){
                    List<DinnerTable> tableList = baseResponse.getData().getData();
                    if (tableList!=null && tableList.size()>0){
                        onQueryTableStatusListener.onQueryTableStatusSuccess(tableList);
                    }else {
                        onQueryTableStatusListener.onQueryTableStatusFailure("查询餐桌信息为空！");
                    }

                }else{
                    onQueryTableStatusListener.onQueryTableStatusFailure("查询餐桌信息失败！");
                }

            }
            @Override
            public void onFailure(String msg) {
                ToastUtils.showMsg("异常："+msg);

            }
        });
    }

    /**
     * 查询餐桌信息
     * @param tableId
     * @param onQueryTableStatusListener
     */
    @Override
    public void queryTableReserveInfo(int tableId,final onQueryTableReserveListener onQueryTableStatusListener) {
        PosHttpManager.getReserveTableListHttpEexcute(true,tableId,new OkHttpUtils.ResultCallback() {
            @Override
            public void onSuccess(JsonObject response) {
                BaseResponse<TableReserveData> baseResponse = BaseResponse.fromJson(response.toString(), TableReserveData.class);
                if(baseResponse.getCode().equals("0")){
                    List<TableReserve> tableList = baseResponse.getData().getTableReserveList();
                    if (tableList!=null && tableList.size()>0){
                        onQueryTableStatusListener.onQueryTableReserveSuccess(tableList);
                    }else {
                        onQueryTableStatusListener.onQueryTableReserveFailure("查询餐桌预定信息为空！");
                    }

                }else{
                    onQueryTableStatusListener.onQueryTableReserveFailure("查询餐桌预定信息失败！");
                }

            }
            @Override
            public void onFailure(String msg) {
                ToastUtils.showMsg("异常："+msg);

            }
        });
    }

    /**
     *  更新餐桌状态
     * @param tableId
     * @param tableStatus
     * @param mListenter
     */
    public void clearTableStatus(int tableId,int tableStatus,final onClearTableStatusListener mListenter) {
        int shopId =-1;
        if(!CommonUtils.isNullOrEmpty(ErpCommonData.loadConfig.getShopId()))
            shopId = Integer.parseInt(ErpCommonData.loadConfig.getShopId());
        PosHttpManager.updataTableStatusHttpEexcute(shopId,tableId,tableStatus,new OkHttpUtils.ResultCallback() {
            @Override
            public void onSuccess(JsonObject response) {
                BaseResponse<String> baseResponse = BaseResponse.fromJson(response.toString(), String.class);
                if(baseResponse.getCode().equals("0")){
                    mListenter.onClearTableStatusSuccess();
                }else{
                    mListenter.onClearTableStatusFailure("更新餐桌状态信息失败！");
                }

            }
            @Override
            public void onFailure(String msg) {
                ToastUtils.showMsg("异常："+msg);

            }
        });
    }


}
